package com.jshan.blog.iterceptor;

import cn.dev33.satoken.stp.StpInterface;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.jshan.blog.constans.AuthUserInfoConst;
import com.jshan.blog.utils.RedisUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
@Slf4j
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private RedisUtil redisUtil;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // log.info("getPermissionList:{}", loginType);
        return getAuth(loginId.toString(), loginType);
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return getAuth(loginId.toString(), loginType);
    }

    private List<String> getAuth(String loginId, String loginType) {
        List<String> list = new ArrayList<>();
        String val = (String) redisUtil.getValue(AuthUserInfoConst.AUTH_INFO_KEY + loginId);
        if (val != null) {
            list = JSON.parseObject(val, new TypeReference<List<String>>() {});
        }
        return list;
    }
}
